package com.ablife.limiter.controller;

import com.ablife.limiter.algorithm.CountRateLimiter;
import com.ablife.limiter.service.ReturnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RateLimiterController {
    private static final Logger logger = LoggerFactory.getLogger(RateLimiterController.class);
    @Autowired
    CountRateLimiter rateLimiter;
    @Autowired
    ReturnService returnService;

    public RateLimiterController() {
    }

    @RequestMapping(
            value = {"/ratelimiter/limit"},
            produces = {"text/html;charset=UTF-8"}
    )
    @ResponseBody
    public String limit(String channel, String tradeCode, String type) {
        channel = "BANK_INSURANCE";
        tradeCode = "EDORAPPLY_CT";
        type = "2";
        if (!StringUtils.isEmpty(channel) && !StringUtils.isEmpty(tradeCode) && !StringUtils.isEmpty(type)) {
            boolean flag = this.rateLimiter.doLimit(channel, tradeCode, type);
            return this.returnService.getReturnString(channel, tradeCode, flag);
        } else {
            return "{\"code\":\"9\",\"description\":\"参数有误\"}";
        }
    }
}
